package com.krain.corelibrary.widget;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.PopupWindow;

/**
 * 带有背景蒙板层的PopupWindow <br/>
 * 需自定义OnDismissListener的，调用构造方法{@link LayerMaskPopupWindow#LayerMaskPopupWindow(Context, OnDismissListener)} <br/>
 * 否则调用默认构造方法{@link LayerMaskPopupWindow#LayerMaskPopupWindow(Context)}
 * 
 * @author EvaGu
 * 
 */
public class LayerMaskPopupWindow extends PopupWindow {

    public static final float DEFAULT_MASK_LAYER_ALPHA = 0.6F;
    public static final int ANIM_TIME = 320;
    public static final int ANIM_REFRESH_TIME = 16;
    public static final double ANIM_COUNT = ANIM_TIME * 1.0 / ANIM_REFRESH_TIME;

    private float             mMaskLayerAlpha          = DEFAULT_MASK_LAYER_ALPHA;

    private Context           mContext;

    public LayerMaskPopupWindow(Context context) {
        super(context);
        init(context, null);
    }

    public LayerMaskPopupWindow(Context context, OnDismissListener listener) {
        super(context);
        init(context, listener);
    }

    private void init(Context context, final OnDismissListener listener) {
        this.mContext = context;
        setWidth(LayoutParams.MATCH_PARENT);
        setHeight(LayoutParams.WRAP_CONTENT);
        setAnimationStyle(android.R.style.Animation_InputMethod);
        setFocusable(true);
        setOutsideTouchable(false);
        setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

        // 适配虚拟键盘的情况，有虚拟键盘的情况下不会遮挡popUpWindow
        setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

        setOnDismissListener(listener);
    }

    @Override
    public void setOnDismissListener(final OnDismissListener onDismissListener) {
        OnDismissListener listener = new OnDismissListener() {
            @Override
            public void onDismiss() {
                mAlphaHandler.removeMessages(0);
                setAlpha(1.0f);
                if (onDismissListener != null) {
                    onDismissListener.onDismiss();
                }
            }
        };

        super.setOnDismissListener(listener);
    }

    /***
     * 设置蒙板透明度
     * 
     * @param alpha
     *            0.0 - 1.0 (0.0 : 不透明 | 1.0 : 全透明)
     */
    public void setMaskLayerAlpha(float alpha) {
        this.mMaskLayerAlpha = alpha;
    }

    @Override
    public void showAtLocation(View parent, int gravity, int x, int y) {
        // 显示蒙板

        if (parent != null ) {
            // 显示蒙板
            setAlpha(mMaskLayerAlpha);
//        runAlphaAnim();
            super.showAtLocation(parent, gravity, x, y);
        }
    }

    private void runAlphaAnim() {
        mAlphaHandler.sendEmptyMessageDelayed(0, ANIM_REFRESH_TIME);
    }

    private float mTempAlpha = 1;
    private Handler mAlphaHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void dispatchMessage(Message msg) {
            super.dispatchMessage(msg);

            double delta = mMaskLayerAlpha * 1.0 / ANIM_COUNT;
            mTempAlpha -= delta;
            setAlpha(mTempAlpha);

            if (mTempAlpha > mMaskLayerAlpha) {
                runAlphaAnim();
            } else {
                mAlphaHandler.removeMessages(0);
            }
        }
    };

    /**
     * 设置蒙板层透明度
     * 
     * @param alpha
     */
    public void setAlpha(float alpha) {
        if ("HUAWEI".equals(Build.BRAND)||"HONOR".equals(Build.BRAND)) {
            //适配HUAWEI机型
            ((Activity) mContext).getWindow().getDecorView().setAlpha(alpha);
        }
        else {
            WindowManager.LayoutParams lp = ((Activity) mContext).getWindow().getAttributes();
            lp.alpha = alpha; // 0.0-1.0
            ((Activity) mContext).getWindow().setAttributes(lp);
        }
    }

}
